<?php
/* 
 * class ini untuk mengelola listing
 * hanya agen yg bisa mengakses
 */
class Listing extends CI_Controller{
    var $_admin_view = '';

    function  __construct() {
        parent::__construct();
        $this->load->model('model_admin', 'madmin');
        $this->load->model('model_listing', 'mlisting');
        $this->load->helper('url','site');
        $this->load->helper(array('form','string'));
        $this->load->library(array('session','table'));
        $this->config->load('artyproperty');
        $this->_admin_view = $this->config->item('admin_view').'wrapper';
    }

    function index(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';$id='';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
            $id = $this->session->userdata('user_id');
        } else {
            redirect(site_url(),'location');
        }

        $data = array('daftar_listing' => $this->mlisting->get_listing_admin($level,$id),
            'page_title' => 'Kelola Properti',
            'page_content' => 'daftar_listing',
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);
    }

    function form_tambah_listing() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
        }else {
            redirect(site_url(),'location');
        }

        $data = array('isUpdate'=>FALSE,
            'page_title' => 'Tambah Properti',
            'page_content' => 'tambah_listing_infodasar',
            'nama' =>'rumah1', 'tipe' =>'1', 'luas' =>'200','jml_kmr_tidur' => '2',
            'jml_kmr_mandi'=>'2','harga'=>'100000000','sewa_beli'=>'1',
            'jml_kmr_pembantu' =>'1',
            'luas_tanah'=>'200','luas_bangunan' => '150',
            'tahun_didirikan' => '2010',
            'tambahan_info_furnitur' => 'lalala',
            'deskripsi' => 'desk',
            'info_sekolah_rs' => 'deket',
            'area_spesifik' => 'strategis', 'jalan' => 'jalan merdeka',
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);
    }
    function tambah_listing_infodasar(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = ''; $level = ''; $id = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
            $id = $this->session->userdata('user_id');
        }else {
            redirect(site_url(),'location');
        }
        $judul_properti = $this->session->userdata('judul');
        
        $data = array('daftar_listing' => $this->mlisting->get_listing_admin($level,$id),
            'page_title' => 'Tambah Properti',
            'page_content' => 'tambah_listing_infodasar',
            'judul_properti' => $judul_properti,
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);   
    }

    function next_form(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = ''; $level = ''; $id = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
            $id = $this->session->userdata('user_id');
        }else {
            redirect(site_url(),'location');
        }
        
        $judul = $this->input->post('judul');

        $this->session->set_userdata('judul',$judul);

        $data = array(
            'page_title' => 'Tambah Properti',
            'page_content' => 'tambah_listing_infotambahan',
            //'judul_properti' => $judul_properti,
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user
            );
        $this->load->view($this->_admin_view,$data);
    }

    function tambah_listing_infotambahan(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = ''; $level = ''; $id = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
            $id = $this->session->userdata('user_id');
        }else {
            redirect(site_url(),'location');
        }


        $jenis_iklan = $this->input->post('sewa_beli');
        $judul_properti = $this->input->post('judul');
        $jenis_properti = $this->input->post('tipe');
        $luas_tanah = $this->input->post('luas_tanah');
        $luas_bangunan = $this->input->post('luas_bangunan');
        $jml_kmr_tidur = $this->input->post('jml_kmr_tidur');
        $jml_kmr_mandi = $this->input->post('jml_kmr_mandi');
        $jml_kmr_pembantu = $this->input->post('jml_kmr_pembantu');
        $harga = $this->input->post('harga');
        $sertifikasi = $this->input->post('sertifikasi');
        $tahun_didirikan = $this->input->post('tahun_didirikan');
        $furnitur = $this->input->post('furnitur');
        $tambahan_info_furnitur = $this->input->post('tambahan_info_furnitur');
        $provinsi = $this->input->post('provinsi');
        $kota= $this->input->post('kota');

        $this->session->set_userdata('sewa_beli',$jenis_iklan);
        $this->session->set_userdata('judul',$judul_properti);
        $this->session->set_userdata('tipe',$jenis_properti);
        $this->session->set_userdata('luas_tanah',$luas_tanah);
        $this->session->set_userdata('luas_bangunan',$luas_bangunan);
        $this->session->set_userdata('jml_kmr_mandi',$jml_kmr_mandi);
        $this->session->set_userdata('jml_kmr_tidur',$jml_kmr_tidur);
        $this->session->set_userdata('jml_kmr_pembantu',$jml_kmr_pembantu);
        $this->session->set_userdata('harga',$harga);
        $this->session->set_userdata('sertifikasi',$sertifikasi);
        $this->session->set_userdata('tahun_didirikan',$tahun_didirikan);
        $this->session->set_userdata('furnitur',$furnitur);
        $this->session->set_userdata('tambahan_info_furnitur',$tambahan_info_furnitur);
        $this->session->set_userdata('provinsi',$provinsi);
        $this->session->set_userdata('kota',$kota);
        
        $data = array('daftar_listing' => $this->mlisting->get_listing_admin($level,$id),
            'page_title' => 'Tambah Properti',
            'page_content' => 'tambah_listing_infotambahan',
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);
    }

    function tambah_listing_galeri(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = ''; $level = ''; $id = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
            $id = $this->session->userdata('user_id');
        }else {
            redirect(site_url(),'location');
        }
        
        $deskripsi = $this->input->post('deskripsi');
        $info_sekolah_rs = $this->input->post('info_sekolah_rs');
        $jalan = $this->input->post('jalan');
        $area_spesifik = $this->input->post('area_spesifik');
        $kolam_renang = $this->input->post('kolam_renang');
        $gym = $this->input->post('gym');
        $jacuzzi = $this->input->post('jacuzzi');
        $tennis_court = $this->input->post('tennis_court');
        $jogging_track = $this->input->post('jogging_track');
        $function_room = $this->input->post('function_room');
        $children_playground = $this->input->post('children_playground');
        $atm = $this->input->post('atm');
        $mini_market = $this->input->post('mini_market');
        $library = $this->input->post('library');
        $shopping_center = $this->input->post('shopping_center');
        $entertainment_center = $this->input->post('entertainment_center');

        $this->session->set_userdata('deskripsi',$deskripsi);
        $this->session->set_userdata('info_sekolah_rs',$info_sekolah_rs);
        $this->session->set_userdata('jalan',$jalan);
        $this->session->set_userdata('area_spesifik',$area_spesifik);
        $this->session->set_userdata('kolam_renang',$kolam_renang);
        $this->session->set_userdata('gym',$gym);
        $this->session->set_userdata('jacuzzi',$jacuzzi);
        $this->session->set_userdata('tennis_court',$tennis_court);
        $this->session->set_userdata('function_room',$function_room);
        $this->session->set_userdata('children_playground',$children_playground);
        $this->session->set_userdata('atm',$atm);
        $this->session->set_userdata('mini_market',$mini_market);
        $this->session->set_userdata('library',$library);
        $this->session->set_userdata('shopping_center',$shopping_center);
        $this->session->set_userdata('entertainment_center',$entertainment_center);

        $data = array(
            'page_title' => 'Tambah Properti',
            'page_content' => 'tambah_listing_galeri',
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user
            );
        $this->load->view($this->_admin_view,$data);
    }

    function tambah_listing(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = ''; $level = ''; $id = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
            $id = $this->session->userdata('user_id');
        }else {
            redirect(site_url(),'location');
        }
        //ambil dari method POST
        $reg['sewa_beli'] = $this->input->userdata('sewa_beli');
        $reg['nama_properti'] = $this->input->userdata('judul');
        $reg['tipe'] = $this->input->userdata('tipe');
        $reg['luas_tanah'] = $this->input->userdata('luas_tanah');
        $reg['luas_bangunan'] = $this->input->userdata('luas_bangunan');
        $reg['jml_kmr_tidur'] = $this->input->userdata('jml_kmr_tidur');
        $reg['jml_kmr_mandi'] = $this->input->userdata('jml_kmr_mandi');
        $reg['jml_kmr_pembantu'] = $this->input->userdata('jml_kmr_pembantu');
        $reg['harga'] = $this->input->userdata('harga');        
        $reg['sertifikasi'] = $this->input->userdata('sertifikasi');
        $reg['tahun_didirikan'] = $this->input->userdata('tahun_didirikan');
        $reg['furnitur'] = $this->input->userdata('furnitur');
        $reg['tambahan_info_furnitur'] = $this->input->userdata('tambahan_info_furnitur');
        $reg['deskripsi'] = $this->input->userdata('deskripsi');
        $reg['info_sekolah_rs'] = $this->input->userdata('info_sekolah_rs');
        $reg['area_spesifik'] = $this->input->userdata('area_spesifik');        
        $reg['info_interior'] = $this->input->userdata('info_interior');

        //untuk fasilitas
        $fas['kolam_renang'] = 0;
        $fas['gym'] = 0;
        $fas['sauna'] = 0;
        $fas['tennis_court'] = 0;
        $fas['jogging_track'] = 0;
        $fas['function_room'] = 0;
        $fas['children_playground'] = 0;
        $fas['atm'] = 0;
        $fas['mini_market'] = 0;
        $fas['library'] = 0;
        $fas['shopping_center'] = 0;
        $fas['entertainment_center'] = 0;

        if($this->input->post('kolam_renang')==1){
            $fas['kolam_renang'] = 1;
        }
        if($this->input->post('gym')==1){
            $fas['gym'] = 1;
        }
        if($this->input->post('jacuzzi')==1){
            $fas['sauna'] = 1;
        }
        if($this->input->post('tennis_court')==1){
            $fas['tennis_court'] = 1;
        }
        if($this->input->post('jogging_track')==1){
            $fas['jogging_track'] = 1;
        }
        if($this->input->post('function_room')==1){
            $fas['function_room'] = 1;
        }
        if($this->input->post('children_playground')==1){
            $fas['children_playground'] = 1;
        }
        if($this->input->post('atm')==1){
            $fas['atm'] = 1;
        }
        if($this->input->post('mini_market')==1){
            $fas['mini_market'] = 1;
        }
        if($this->input->post('library')==1){
            $fas['library'] = 1;
        }
        if($this->input->post('shopping_center')==1){
            $fas['shopping_center'] = 1;
        }
        if($this->input->post('entertaintment_center')==1){
            $fas['entertainment_center'] = 1;
        }
        
        $this->db->insert('fasilitas', $fas);

        $query = "select fasilitas_id from fasilitas order by fasilitas_id desc limit 1";
        $row_id = $this->db->query($query);
        $fasilitas_id = $row_id->row()->fasilitas_id;
        $reg['fasilitas_id'] = $fasilitas_id; //ini dummy default fasilitasnya 4 (nanti harus berdasar input ya)
        
        //untuk user id
        $reg['user_id'] = $id; 

        //untuk alamat
        $add['provinsi'] = $this->input->post('provinsi');
        $add['kota_kab'] = $this->input->post('kota');
        $add['jalan'] = $this->input->post('jalan');

        $this->db->insert('alamat',$add);
        $query = "select alamat_id from alamat order by alamat_id desc limit 1";
        $row_id = $this->db->query($query);
        $alamat_id = $row_id->row()->alamat_id;

        $reg['alamat_id'] = $alamat_id;  //ini dummy default alamatnya di 3 (nanti harus berdasar input ya)
        
        //kode propertinya random
        $reg['kode'] = strtolower(random_string('alnum',8));
        
        

        //cek validasi masukkan
        if ($this->validation()) {
            $this->db->trans_begin();
            $this->db->insert('properti', $reg);
            if ($this->db->trans_status() == FALSE) {
                $this->db->trans_rollback();
            } else {
                $this->db->trans_commit();
                $query = "select properti_id from properti order by properti_id desc limit 1";
                $row_id = $this->db->query($query);
                $properti_id = $row_id->row()->properti_id;
                //ini untuk upload gambar
                $gal = count($_FILES['img']['name']); // hitung total $_FILES['img']
                //echo $gal;
                for($i=0;$i<$gal;$i++)
                {
                    $_FILES['img'.$i]['name']          = $_FILES['img']['name'][$i];
                    $_FILES['img'.$i]['type']            = $_FILES['img']['type'][$i];
                    $_FILES['img'.$i]['tmp_name']  = $_FILES['img']['tmp_name'][$i];
                    $_FILES['img'.$i]['error']           = $_FILES['img']['error'][$i];
                    $_FILES['img'.$i]['size']             = $_FILES['img']['size'][$i];
                    $config['upload_path']                = 'pictures/properti/';
                    $config['max_size']                  = '2048';
                    $config['allowed_types']             = 'gif|jpeg|jpg|png';
                    $config['file_name']                     = sha1(md5(date('YmdHis'))).$i.$properti_id;
                    $this->load->library('upload', $config);
                    if($this->upload->do_upload("img".$i)){
                        $result = $this->upload->data();
                        $input_data['url'] = $config['upload_path'].$config['file_name'].$result['file_ext'];
                        $input_data['judul_media'] = $reg['nama_properti'];
                        $input_data['tgl_upload'] = date('YmdHis');
                        $input_data['properti_id'] = $properti_id;
                        $this->db->insert('media', $input_data);
                    }
                }
                redirect('admin/listing','location');
            }
        } else {
            $data = array('page_content' => 'tambah_listing',
                            'page_title' => 'Error | Tambah Properti',
                            'level_user' => $this->session->userdata('level'),
                            'user' => $this->session->userdata('username'),
                            'isLogin' => TRUE,'login'=>'',
                            'nama' =>'', 'tipe' =>'', 'luas' =>'','jml_kmr_tidur' => '',
                            'jml_kmr_mandi'=>'','harga'=>'','sewa_beli'=>'', 'luas_tanah'=>'',
                            'luas_bangunan' => '',
                            'jml_kmr_pembantu' =>'',
                            'password' => '','isUpdate' => FALSE,
                            'note'=>'');
        }

        $this->load->view($this->_admin_view, $data);  //tampilkan datanya
    }

    function form_update_listing(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = ''; $level=''; $id='';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
            $id = $this->session->userdata('user_id');
        }else {
            redirect(site_url(),'location');
        }
        
        $properti_id = '';
        if ($this->uri->segment(4) == FALSE) {
            redirect('listing', 'location');
        } else {
            $properti_id = $this->uri->segment(4);
        }

        //$query = $this->mlisting->get_properti(array('properti_id'=>$properti_id));
        $query = $this->db->query("select * from properti where properti_id = ?",$properti_id);
        $udata = $query->row();

        $query = $this->db->query("select * from alamat,properti where properti_id = ? and properti.alamat_id = alamat.alamat_id",$properti_id);
        $alamat = $query->row();

        $query = $this->db->query("select * from fasilitas,properti where properti_id = ? and properti.fasilitas_id = fasilitas.fasilitas_id",$properti_id);
        $fasdata = $query->row(); 

        $output['kolam_renang'] = $fasdata->kolam_renang;
        $output['gym'] = $fasdata->gym;
        $output['sauna'] = $fasdata->sauna;     
        $output['tennis_court'] = $fasdata->tennis_court;
        $output['jogging_track'] = $fasdata->jogging_track;
        $output['function_room'] = $fasdata->function_room;
        $output['atm'] = $fasdata->atm;
        $output['children_playground'] = $fasdata->children_playground;
        $output['entertainment_center'] = $fasdata->entertainment_center;
        $output['mini_market'] = $fasdata->mini_market;
        $output['library'] = $fasdata->library;
        $output['shopping_center'] = $fasdata->shopping_center;  

        $data = array(
            'isUpdate'=>TRUE, 'output' => $output,
            'page_title' => 'Ubah Properti',
            'page_content' => 'update_listing',
            'properti_id' => $udata->properti_id,
            'nama' => $udata->nama_properti, 'tipe' => $udata->tipe,'luas_bangunan'=> $udata->luas_bangunan,
            'jml_kmr_tidur' => $udata->jml_kmr_tidur, 'jml_kmr_mandi'=>$udata->jml_kmr_mandi,
            'jml_kmr_pembantu'=>$udata->jml_kmr_pembantu,
            'luas_tanah'=> $udata->luas_tanah,
            'harga' => $udata->harga,'sewa_beli' => $udata->sewa_beli,
            'sertifikat' => $udata->sertifikasi,
            'furn' => $udata->furnitur,
            'tahun_didirikan' => $udata->tahun_didirikan,
            'tambahan_info_furnitur' => $udata->tambahan_info_furnitur,
            'deskripsi' => $udata->deskripsi,
            'info_sekolah_rs' => $udata->info_sekolah_rs,
            'area_spesifik' => $udata->area_spesifik,
            'jalan' => $alamat->jalan,
            'prov' => $alamat->provinsi,
            'kot_kab' => $alamat->kota_kab,
            'isLogin' => $islogin, 'level_user' => $level, 'id' => $id, 'properti_id' => $properti_id,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);
    }

    function update_listing(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = ''; $level = ''; $id = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
            $id = $this->session->userdata('user_id');
        }else {
            redirect(site_url(),'location');
        }

        $properti_id='';
        if ($this->uri->segment(4) == FALSE) {
            redirect('listing', 'location');
        } else {
            $properti_id = $this->uri->segment(4);
        }

        //ambil dari method POST
        $reg['sewa_beli'] = $this->input->post('sewa_beli',true);
        $reg['nama_properti'] = $this->input->post('judul',true);
        $reg['tipe'] = $this->input->post('tipe',true);
        $reg['luas_tanah'] = $this->input->post('luas_tanah',true);
        $reg['luas_bangunan'] = $this->input->post('luas_bangunan',true);
        $reg['jml_kmr_tidur'] = $this->input->post('jml_kmr_tidur',true);
        $reg['jml_kmr_mandi'] = $this->input->post('jml_kmr_mandi',true);
        $reg['jml_kmr_pembantu'] = $this->input->post('jml_kmr_pembantu',true);
        $reg['harga'] = $this->input->post('harga',true);        
        $reg['sertifikasi'] = $this->input->post('sertifikasi',true);
        $reg['tahun_didirikan'] = $this->input->post('tahun_didirikan',true);
        $reg['furnitur'] = $this->input->post('furnitur',true);
        $reg['tambahan_info_furnitur'] = $this->input->post('tambahan_info_furnitur',true);
        $reg['deskripsi'] = $this->input->post('deskripsi',true);
        $reg['info_sekolah_rs'] = $this->input->post('info_sekolah_rs',true);
        $reg['area_spesifik'] = $this->input->post('area_spesifik',true);        
        $reg['info_interior'] = $this->input->post('info_interior',true);

        //untuk fasilitas
        $fas['kolam_renang'] = 0;
        $fas['gym'] = 0;
        $fas['sauna'] = 0;
        $fas['tennis_court'] = 0;
        $fas['jogging_track'] = 0;
        $fas['function_room'] = 0;
        $fas['children_playground'] = 0;
        $fas['atm'] = 0;
        $fas['mini_market'] = 0;
        $fas['library'] = 0;
        $fas['shopping_center'] = 0;
        $fas['entertainment_center'] = 0;

        if($this->input->post('kolam_renang')==1){
            $fas['kolam_renang'] = 1;
        }
        if($this->input->post('gym')==1){
            $fas['gym'] = 1;
        }
        if($this->input->post('jacuzzi')==1){
            $fas['sauna'] = 1;
        }
        if($this->input->post('tennis_court')==1){
            $fas['tennis_court'] = 1;
        }
        if($this->input->post('jogging_track')==1){
            $fas['jogging_track'] = 1;
        }
        if($this->input->post('function_room')==1){
            $fas['function_room'] = 1;
        }
        if($this->input->post('children_playground')==1){
            $fas['children_playground'] = 1;
        }
        if($this->input->post('atm')==1){
            $fas['atm'] = 1;
        }
        if($this->input->post('mini_market')==1){
            $fas['mini_market'] = 1;
        }
        if($this->input->post('library')==1){
            $fas['library'] = 1;
        }
        if($this->input->post('shopping_center')==1){
            $fas['shopping_center'] = 1;
        }
        if($this->input->post('entertaintment_center')==1){
            $fas['entertainment_center'] = 1;
        }
        
        //$this->db->insert('fasilitas', $fas);

        $query = "select * from fasilitas,properti where properti.properti_id = ? and properti.fasilitas_id = fasilitas.fasilitas_id";
        $row_id = $this->db->query($query,$properti_id);
        $fasilitas_id = $row_id->row()->fasilitas_id;
        //$reg['fasilitas_id'] = $fasilitas_id; //ini dummy default fasilitasnya 4 (nanti harus berdasar input ya)
        $this->db->where('fasilitas_id',$fasilitas_id);
        $this->db->update('fasilitas',$fas);    

        //untuk user id
        //$reg['user_id'] = $id; 

        //untuk alamat
        $add['provinsi'] = $this->input->post('provinsi');
        $add['kota_kab'] = $this->input->post('kota');
        $add['jalan'] = $this->input->post('jalan');

        //$this->db->insert('alamat',$add);
        $query = "select * from alamat,properti where properti.properti_id = ? and properti.alamat_id = alamat.alamat_id";
        $row_id = $this->db->query($query,$properti_id);
        $alamat_id = $row_id->row()->alamat_id;
        $this->db->where('alamat_id',$alamat_id);
        $this->db->update('alamat',$add);

        //$reg['alamat_id'] = $alamat_id;  //ini dummy default alamatnya di 3 (nanti harus berdasar input ya)
        
        //kode propertinya random
        //$reg['kode'] = strtolower(random_string('alnum',8));
        

        //cek validasi masukkan
        // if ($this->validation()) {
        //     $this->db->trans_begin();
        //     $this->db->where('properti_id',$properti_id);
        //     $this->db->update('properti', $reg);
        //     if ($this->db->trans_status() == FALSE) {
        //         $this->db->trans_rollback();
        //     } else {
        //         $this->db->trans_commit();
        //         redirect('admin/listing','location');
        //     }
        // } else {
        //     $data = array('page_content' => 'tambah_listing',
        //                     'page_title' => 'Error | update listing',
        //                     'level_user' => $this->session->userdata('level'),
        //                     'user' => $this->session->userdata('username'),
        //                     'isLogin' => TRUE,'login'=>'',
        //                     'nama' =>'', 'tipe' =>'', 'luas' =>'','jml_kmr_tidur' => '',
        //                     'jml_kmr_mandi'=>'','harga'=>'','sewa_beli'=>'', 'luas_tanah'=>'',
        //                     'luas_bangunan' => '',
        //                     'jml_kmr_pembantu' =>'',
        //                     'password' => '','isUpdate' => TRUE,
        //                     'note'=>'');
        // }
        $this->db->where('properti_id',$properti_id);
        $this->db->update('properti', $reg);
        $this->index();
        //$this->load->view($this->_admin_view, $data);  //tampilkan datanya
    }

    function hapus_listing(){
        $listing_id = $this->uri->segment(4);        

        if(!is_null($listing_id) AND $listing_id != 0){
            $this->mlisting->hapus_listing(array('properti_id'=>$listing_id));
            $this->db->delete('media',array('properti_id'=>$listing_id));
        }
        redirect('admin/listing','location');
    }

    public function validation($str=''){
        $this->load->library('form_validation');

        $this->valid = $this->form_validation;

        //konfigurasi aturan2 masukkan valid
        $this->valid->set_rules('judul','judul','trim|required|min_length[4]|max_length[20]');
        $this->valid->set_rules('tipe','tipe','trim|required|min_length[1]|max_length[1]');
        $this->valid->set_rules('luas_tanah','luas_tanah','trim|required|min_length[1]|max_length[5]');
        $this->valid->set_rules('jml_kmr_tidur','jml_kmr_tidur','trim|required|min_length[1]|max_length[3]');
        $this->valid->set_rules('jml_kmr_mandi','jml_kmr_mandi','trim|required|min_length[1]|max_length[3]');
        $this->valid->set_rules('harga','harga','trim|required|min_length[4]|max_length[20]');
        $this->valid->set_rules('sewa_beli','sewa_beli','trim|required|min_length[1]|max_length[2]');
        //end konfigurasi

        return ($this->form_validation->run()==FALSE) ? FALSE : TRUE;
    }

    /**
     * fungsi untuk mengecek duplikasi username pada database
     * @param <type> $username
     * @return <type>
     */
    public function spare_username($username){
        $sql = "SELECT username
                FROM user
                WHERE username=?";
        $query = $this->db->query($sql, array($username));
        $this->valid->set_message('spare_username',"Nama $username sudah ada. Silakan isi dengan username yg lain");
        return ($query->num_rows()==0)? TRUE : FALSE;

    }

    function generate_kota(){
        $kota = array('jakarta' => array('jakarta selatan', 'jakarta pusat', 'jakarta utara'),
            'jateng' => array('tegal', 'semarang', 'purwokerto'),
            'jabar' => array('bandung', 'bogor', 'depok'),
            'default'=>array('-- pilih kota --'));

        $opsi = $_POST['kota'];
        $sel = $_GET['sel'];
        $arr = $kota[$opsi];

        foreach ($arr as $key => $val) {
            echo $sel;
            echo '<option value="' . $val . '">' . $val . '</option>';
        }
    }
}
?>
